﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Coolite.Ext.Web;
using System.Data;
using System.Web;
using Newtonsoft.Json;

namespace AjaxLibrary
{
    class productwithCategory : System.Web.IHttpHandler
    {
        #region IHttpHandler 成员

        public bool IsReusable
        {
            get { return true; }
        }

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/json";
            string nodeId = context.Request["node"];

            if (!string.IsNullOrEmpty(nodeId))
            {
                DataTable dt = new DataTable();
                String[] ids = nodeId.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

                #region 类别查询
                if (ids[0] == "0")
                {
                    int category = 0;
                    int.TryParse(ids[1], out category);

                    if (category != 0)
                    {
                        dt = logic.category.getByParent(category);
                    }
                    else
                    {
                        dt = logic.category.getByParent(0);
                    }

                    using (JsonTextWriter writer = new JsonTextWriter(context.Response.Output))
                    {
                        writer.WriteStartArray();

                        foreach (DataRow row in dt.Rows)
                        {
                            writer.WriteStartObject();
                            writer.WritePropertyName("id");
                            if (row["treelevel"].ToString() == "1" && row["parentId"].ToString() != "0")
                            {
                                writer.WriteValue("1," + row["categoryId"]);
                            }
                            else
                            {
                                writer.WriteValue("0," + row["categoryId"]);
                            }
                            writer.WritePropertyName("text");
                            writer.WriteValue(row["name"]);
                            writer.WriteEndObject();
                        }

                        writer.WriteEndArray();
                    }
                }
                #endregion

                #region 商品查询
                else
                {
                    // 查询商品
                    dt = logic.product.getByCategory(Convert.ToInt32(ids[1]));
                    using (JsonTextWriter writer = new JsonTextWriter(context.Response.Output))
                    {
                        writer.WriteStartArray();

                        foreach (DataRow row in dt.Rows)
                        {
                            writer.WriteStartObject();
                            writer.WritePropertyName("id");
                            writer.WriteValue("1" + row["productId"]);
                            writer.WritePropertyName("text");
                            writer.WriteValue(row["name"]);
                            writer.WritePropertyName("leaf");
                            writer.WriteValue(true);
                            writer.WritePropertyName("href");
                            writer.WriteValue(String.Format("javascript:callbackFunction({0});", row["productId"]));
                            writer.WriteEndObject();
                        }

                        writer.WriteEndArray();
                    }
                }
                #endregion
            }
        }

        #endregion
    }
}
